/**
 * Created by WYH on 17/1/8.
 */


/* 封装所有接口 */
var Api = (function () {
    var assigned = window.localStorage.assign;


    /*获取验证码*/
    function fetchVerificationCode(phone, sucFuc, errFuc) {
        var data = {
            "value": phone
        };
        Service.postAjax("verifications/phone", data, sucFuc, errFuc, false);
    }

    /*重置密码*/
    function changeCompanyPassword(oldPassword, newPassword, sucFuc, errFuc) {
        var path = "companies/" + assigned + "/password";
        data = {
            "password": oldPassword,
            "newPassword": newPassword
        };
        Service.putAjax(path, data,  sucFuc, errFuc, true);
    }


    /*修改手机号*/
    function changeCompanyPhone(newPhone, verifyCode ,sucFuc, errFuc) {
        var path = "companies/" + assigned + "/phone";
        var body = {
            phone: newPhone,
            verificationCode: verifyCode
        };
        Service.putAjax(path, body, sucFuc, errFuc, true);
    }

    /*注册企业用户*/
    function registryCompanies(companyDetail, sucFuc, errFuc) {
        Service.postAjax("companies/registry", companyDetail, sucFuc, errFuc, false);
    }

    /*登录*/
    function login(phone, password, sucFuc, errFuc) {
        var path = "companies/" + "login";
        var data = {
            "phone": phone,
            "password": password
        };
        Service.postAjax(path, data, sucFuc, errFuc, false );
    }

    /*查询公司员工列表*/
    function fetchCompaniesStaff(contidion, limitSize, sortSkip ,sucFuc, errFuc) {
        if (contidion != null) {
            contidion = contidion.indexOf(":") == -1 ? contidion : "condition=" + contidion;
        } else {
            contidion = "";
        }
        console.log(contidion);
        limitSize = limitSize == null? "" : "&limitSize=" + limitSize;
        sortSkip = sortSkip == null ? "" : "&sortSkip=" + sortSkip;
        var path = "companies/" + assigned + "/staffs?" + contidion + limitSize + sortSkip;
        console.log(path);
        Service.getAjax(path, sucFuc, errFuc);
    }

    /*添加公司员工*/
    function addCompaniesStaff(staffData, sucFuc, errFuc) {
        var path = "companies/" + assigned + "/staffs";
        Service.postAjax(path, staffData, sucFuc, errFuc, true);
    }

    /*修改员工信息*/
    function changeStaffInfo(staffId, staffData, sucFuc, errFuc) {
        var keyPath = "";
        Object.keys(staffData).forEach(function (key, index) {
            if (index == 0) {
                keyPath += key;
            } else {
                keyPath += "," + key
            }
        });

        var path = "companies/" + assigned + "/staffs/" + staffId + "/" + keyPath;
        console.log(path);
        Service.putAjax(path, staffData, sucFuc, errFuc, true);
    }

    /*删除员工*/
    function deleteCompanyStaff(staffIds, sucFuc, errFuc) {
        var path = "companies/" + assigned + "/staffs/" + staffIds;
        console.log(path);
        Service.deleteAjax(path, sucFuc, errFuc, true);
    }

    // 忘记密码
    function setupForgetPassword(phone, password, vcode, sucFuc, errFuc) {
        var path = "companies/password?phone=" + phone +"&vcode=" + vcode;
        data = {
            "value": password
        };
        Service.putAjax(path, data, sucFuc, errFuc, false);
    }

    /*忘记密码中验证身份*/
    function verifyPhone(phone, verificationCode, sucFuc, errFuc) {
        var path = "companies/verify_phone" + "/" + phone + "/" + verificationCode;
        Service.getAjax(path, sucFuc, errFuc);
    }

    // 获取企业信息
    function fetchCompanyDetailInfo(sucFuc, errFuc) {
        var path = "companies/" + assigned;
        Service.getAjax(path, sucFuc, errFuc);
    }

    /*获取社区列表*/
    function fetchCommunityList(sucFuc, errFuc) {
        Service.getAjax("communities",sucFuc, errFuc);
    }


    function uploadPicture(contentMD5, sucFuc, errFuc) {
        var path = "oss_authorization/picture?me=" + assigned;
        var body = {
            key: "WEB_image.jpg",
            contentType: "image/jpeg",
            contentMD5: contentMD5
        };
        Service.postAjax(path, body, sucFuc, errFuc, true);
    }


    /*完善公司信息*/
    function perfectCompanyInfo(companyDetail, sucFuc, errFuc) {
        var keyPath = "";
        Object.keys(companyDetail).forEach(function (key, index) {
            if (index == 0) {
                keyPath += key;
            } else {
                keyPath += "," + key
            }
        });
        var path = "companies/" + assigned + "/" + keyPath;
        Service.putAjax(path, companyDetail, sucFuc, errFuc, true);
    }

    return {
        fetchVerificationCode: fetchVerificationCode,
        registerCompanies: registryCompanies,
        login: login,
        fetchCompaniesStaff: fetchCompaniesStaff,
        addCompaniesStaff: addCompaniesStaff,
        changeStaffInfo: changeStaffInfo,
        deleteCompanyStaff: deleteCompanyStaff,
        changeCompanyPassword: changeCompanyPassword,
        changeCompanyPhone: changeCompanyPhone,
        setupForgetPassword: setupForgetPassword,
        verifyPhone: verifyPhone,
        fetchCompanyDetailInfo: fetchCompanyDetailInfo,
        perfectCompanyInfo: perfectCompanyInfo,
        fetchCommunityList: fetchCommunityList,
        uploadPicture: uploadPicture
    }

})();